package com.jason.base;

import com.alibaba.fastjson.JSON;
import com.jason.core.support.HttpCode;
import com.jason.core.util.InstanceUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

public class BaseProcess {

    public static Logger logger = LogManager.getLogger();

    public static Object setSuccessResult(){
        return setSuccessResult(InstanceUtil.newHashMap());
    }

    public static Object setSuccessResult(String msg){
        return setSuccessResult(InstanceUtil.newHashMap(), msg);
    }

    public static Object setSuccessResult(Object data){
        return setSuccessResult(data, HttpCode.OK.msg());
    }

    public static Object setSuccessResult(Object data, String msg){
        return setResult(data, HttpCode.OK, msg);
    }

    public static Object setForbiddenResult(){
        return setResult(InstanceUtil.newHashMap(), HttpCode.FORBIDDEN, HttpCode.FORBIDDEN.msg());
    }

    public static Object setLoginFailResult(){
        return setResult(InstanceUtil.newHashMap(), HttpCode.LOGIN_FAIL, HttpCode.LOGIN_FAIL.msg());
    }

    public static Object setUnAuthorizedResult(){
        return setResult(InstanceUtil.newHashMap(), HttpCode.UNAUTHORIZED, HttpCode.UNAUTHORIZED.msg());
    }

    public static Object setErrorResult(String msg){
        return setResult(InstanceUtil.newHashMap(), HttpCode.INTERNAL_SERVER_ERROR, msg);
    }

    public static Object setErrorResult(){
        return setResult(InstanceUtil.newHashMap(), HttpCode.INTERNAL_SERVER_ERROR, HttpCode.INTERNAL_SERVER_ERROR.msg());
    }

    public static Object setResult(Object data, HttpCode httpCode, String msg){
        Map<String, Object> resultMap = InstanceUtil.newHashMap();
        resultMap.put("data", data);
        resultMap.put("code", httpCode.value());
        resultMap.put("msg", msg);
        resultMap.put("timestamp", System.currentTimeMillis());
        logger.info("RESPONSE : " + JSON.toJSONString(resultMap));
        return resultMap;
    }

}
